/*
 * ReciteAnalysisPlot.cpp
 *
 *  Created on: Jun 21, 2009
 *      Author: walter
 */

#include "ReciteAnalysisPlot.h"

ReciteAnalysisPlot::ReciteAnalysisPlot(QDialog* parent):
	QDialog(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(accept()));

	ui.reciteAnalysisPlotTab->clear();
	QWidget* reciteDateWidget = new QWidget(this);
	QGridLayout* reciteDateGrid = new QGridLayout(reciteDateWidget);
	reciteDateGrid->addWidget(&mReciteNewDatePlot, 0, 0);
	reciteDateGrid->addWidget(&mReviewDatePlot, 1, 0);
	reciteDateGrid->setSpacing(0);
	ui.reciteAnalysisPlotTab->addTab(reciteDateWidget, tr("Reicte Date"));
	ui.reciteAnalysisPlotTab->addTab(&mDifficultyLevelDistributionPlot, tr("Difficulty Level Distribution"));
	ui.reciteAnalysisPlotTab->addTab(&mReciteProgressPlot, tr("Recite Progress"));

	this->accept();
}

ReciteAnalysisPlot::~ReciteAnalysisPlot()
{
}

void ReciteAnalysisPlot::drawReciteDateGraph(map<QDate, int>& reviewDateCount, map<QDate, int>& reciteNewDateCount)
{
	mReciteNewDatePlot.clear();
	mReviewDatePlot.clear();

	//cout<<"ReciteAnalysisPlot::drawReciteDateGraph"<<endl;
	//plot reciteNewDateCount
	map<QDate, int>::iterator count_it;
	QDate today = QDate::currentDate();
	QwtArray<QwtDoubleInterval> intervals;
	QwtArray<double> values;

	mReciteNewDatePlot.setCanvasBackground(QColor(Qt::white));
	mReciteNewDatePlot.setTitle(tr("Number of new words recited every day."));

	QwtPlotGrid *reciteNewDatePlotGrid = new QwtPlotGrid;
	reciteNewDatePlotGrid->enableXMin(true);
	reciteNewDatePlotGrid->enableYMin(true);
	reciteNewDatePlotGrid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	reciteNewDatePlotGrid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	reciteNewDatePlotGrid->attach(&mReciteNewDatePlot);

	HistogramItem *reciteNewDateHistogram = new HistogramItem();
	reciteNewDateHistogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reciteNewDateCount.begin(); count_it != reciteNewDateCount.end(); count_it++)
	{
		//cout<<count_it->first.toString("yyyy-MM-dd").toUtf8().data()<<": "<<count_it->second<<endl;
		int interval = today.daysTo(count_it->first);
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);

	}

	reciteNewDateHistogram->setData(QwtIntervalData(intervals, values));
	reciteNewDateHistogram->attach(&mReciteNewDatePlot);

	mReciteNewDatePlot.setAxisAutoScale(QwtPlot::yLeft);
	mReciteNewDatePlot.setAxisAutoScale(QwtPlot::xBottom);
	mReciteNewDatePlot.replot();

	//plot reviewDateCount
	mReviewDatePlot.setCanvasBackground(QColor(Qt::white));
	mReviewDatePlot.setTitle(tr("Number of words reviewed every day"));

	QwtPlotGrid *reviewDatePlotGrid = new QwtPlotGrid;
	reviewDatePlotGrid->enableXMin(true);
	reviewDatePlotGrid->enableYMin(true);
	reviewDatePlotGrid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	reviewDatePlotGrid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	reviewDatePlotGrid->attach(&mReviewDatePlot);

	HistogramItem *reviewDateHistogram = new HistogramItem();
	reviewDateHistogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reviewDateCount.begin(); count_it != reviewDateCount.end(); count_it++)
	{
		//cout<<count_it->first.toString("yyyy-MM-dd").toUtf8().data()<<": "<<count_it->second<<endl;
		int interval = today.daysTo(count_it->first);
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);
	}

	reviewDateHistogram->setData(QwtIntervalData(intervals, values));
	reviewDateHistogram->attach(&mReviewDatePlot);

	mReviewDatePlot.setAxisAutoScale(QwtPlot::yLeft);
	mReviewDatePlot.setAxisAutoScale(QwtPlot::xBottom);
	mReviewDatePlot.replot();
}

void ReciteAnalysisPlot::drawDifficultyLevelDistribution(map<int, int>& diffLevelDist)
{
	mDifficultyLevelDistributionPlot.clear();

	map<int, int>::iterator count_it;
	QwtArray<QwtDoubleInterval> intervals;
	QwtArray<double> values;

	mDifficultyLevelDistributionPlot.setCanvasBackground(QColor(Qt::white));
	mDifficultyLevelDistributionPlot.setTitle(tr("Difficulty Level Distribution"));

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableXMin(true);
	grid->enableYMin(true);
	grid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(&mDifficultyLevelDistributionPlot);

	HistogramItem *histogram = new HistogramItem();
	histogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = diffLevelDist.begin(); count_it != diffLevelDist.end(); count_it++)
	{
		//cout<<count_it->first.toString("yyyy-MM-dd").toUtf8().data()<<": "<<count_it->second<<endl;
		int interval =count_it->first;
		int count = count_it->second;

		const int width = 1;
		intervals.push_back(QwtDoubleInterval(interval, interval + double(width)));
		values.push_back(count);

	}

	histogram->setData(QwtIntervalData(intervals, values));
	histogram->attach(&mDifficultyLevelDistributionPlot);

	mDifficultyLevelDistributionPlot.setAxisAutoScale(QwtPlot::yLeft);
	mDifficultyLevelDistributionPlot.setAxisAutoScale(QwtPlot::xBottom);
	mDifficultyLevelDistributionPlot.replot();
}

void ReciteAnalysisPlot::drawReciteProgress(map<int, int>& reciteProgress)
{
	mReciteProgressPlot.clear();

	map<int, int>::iterator count_it;
	QwtArray<QwtDoubleInterval> intervals;
	QwtArray<double> values;

	mReciteProgressPlot.setCanvasBackground(QColor(Qt::white));
	mReciteProgressPlot.setTitle(tr("Reicte Progress"));

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableXMin(true);
	grid->enableYMin(true);
	grid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(&mReciteProgressPlot);

	HistogramItem *histogram = new HistogramItem();
	histogram->setColor(Qt::darkCyan);

	intervals.clear();
	values.clear();

	for (count_it = reciteProgress.begin(); count_it != reciteProgress.end(); count_it++)
	{
		int interval =count_it->first;
		int count = count_it->second;

		float width = 0.8f;
		intervals.push_back(QwtDoubleInterval(interval-width/2, interval+width/2));
		values.push_back(count);

	}

	histogram->setData(QwtIntervalData(intervals, values));
	histogram->attach(&mReciteProgressPlot);

	mReciteProgressPlot.setAxisAutoScale(QwtPlot::yLeft);
	mReciteProgressPlot.setAxisTitle(QwtPlot::yLeft, tr("Word Count"));
	mReciteProgressPlot.setAxisAutoScale(QwtPlot::xBottom);
	mReciteProgressPlot.setAxisTitle(QwtPlot::xBottom, tr("Recite Times"));

	mReciteProgressPlot.replot();
}
